#!/bin/bash

function calculate_average {
	case $1 in
		global_cpu_frequency)
			REGEX_MODE='/^(Global,CPU [0-9] Frequency,)/'
			;;
		global_gpu_utilization)
			REGEX_MODE='/^(Global,GPU % Utilization,)/'
			;;
		global_gpu_frequency)
			REGEX_MODE='/^(Global,GPU Frequency,)/'
			;;
		process_cpu_utilization)
			REGEX_MODE='/^(.*,CPU Utilization %,)/'
			;;
		process_fps)
			REGEX_MODE='/^(.*,CPU Utilization %,)/'
			;;
		*)
			echo "default regex mode nothing";;
	esac
	#将shell中的变量值传递给gawk，需要一个单引号以及一个双引号
	#获取gawk的返回值，可以在END{}语句块中print一下获取其返回值。END块只执行一次
	#而BEGIN{}和END{}块之间的{}块每执行完一个文件中的一行会接着执行下一行，直至所有行执行完毕，最后执行END
	counter=`gawk -F, 'BEGIN{counter=0;}'"$REGEX_MODE"'{
		if($5 != 0)
		{
			counter += 1;
		}
	}
	END { print counter; }' $2`
	value_sum=`gawk -F, 'BEGIN{sum=0;}'"$REGEX_MODE"'{
		if($5 != 0)
		{
			sum += $5;
		}
	}
	END { print sum;}' $2`
	if [ $counter != 0 ]
	then
		average=`gawk 'BEGIN{printf "%.4f\n",('$value_sum'/'$counter')}'`
		echo "$average"
	else
		echo "invalid"
	fi
}

function get_process_name {
	REGEX_MODE='/^(.*,CPU Utilization %,)/'
	process_name=`gawk -F, ''"$REGEX_MODE"'{;} END {print $1}' $1`
	echo $process_name
}

function calculate_items {
	list_calculate_items="global_cpu_frequency global_gpu_utilization global_gpu_frequency process_cpu_utilization process_fps"
	file=$1
	counter=0
	for cal_item in $list_calculate_items
	do
		counter=$(expr $counter + 1)
		result=$(calculate_average $cal_item $file)
		case $counter in
			1)
				g_cpu_freq=$result
				;;
			2) 
				g_gpu_util=$result
				;;
			3)
				g_gpu_fre=$result
				;;
			4)
				process_cpu_util=$result
				;;
			5)
			#	echo "process fps:	$result"
				;;
			*)
				;;
		esac
	done
	process_name=$2
	process_fps=$3
	echo "$process_name,$g_cpu_freq,$g_gpu_util,$g_gpu_fre,$process_cpu_util,$process_fps"
}

function calculate_fps {
	REGEX_MODE=$1
	#echo "regex: $REGEX_MODE"
	counter=`gawk -F, 'BEGIN{counter=0;}/'"$REGEX_MODE"'/ && $7 ~ /FPS:/{
		split($7, arr, ":");
		sub(/[[:blank:]]*/, "", arr[2])
		counter += 1;
	}END{print counter}' $2`
#split()以分隔符":"分隔字段，将字段保存在arr数组中
#sub()去掉arr[2]字段中的所有空白符
	sum=`gawk -F, 'BEGIN{sum=0;}/'"$REGEX_MODE"'/ && $7 ~ /FPS:/{
		split($7, arr, ":");
		sub(/[[:blank:]]*/, "", arr[2])
		sum += arr[2];
	}END{print sum}' $2`
	
	if [ $counter != 0 ]
	then
		average=`gawk 'BEGIN{printf "%.4f\n",('$sum'/'$counter')}'`
		echo "$average"
	else
		echo "invalid"
	fi
}
#使用exec命令将标准输出文件描述符重定向到文件(整个shell脚本执行期间均有效)
exec 1>parseresult.csv
echo "process,global cpu frequency,global gpu utilization,global gpu frequency,process cpu utilization,process fps"

# $1是命令行传递的一个目录名，并且目录名后不跟‘/’
if [ -d $1 ]
then
#迭代$1目录下的所有条目(文件或者目录)
	for iterator_dir in $1/*
	do
#处理迭代的目录
		if [ -d $iterator_dir ]
		then
#处理目录中的log文件和非log文件
			for iterator_file in $iterator_dir/*
			do
				is_not_log_file=`echo $iterator_file | gawk -F/ '/^.*[^(log)](.csv)$/{print $3;}'`
				if [ -z $is_not_log_file ]
				then
					#$is_not_log_file的长度为0,则调用continue命令继续迭代，直至迭代到非log文件。
					continue
				else
					#$0表示文件的相对于在命令行传递的目录参数(如Log)$1的完整路径名
					not_log_file=`echo $iterator_file | gawk -F/ '/^.*[^(log)](.csv)$/{print $0;}'`
				fi
			done

			for iterator_file in $iterator_dir/*
			do
				is_log_file=`echo $iterator_file | gawk -F/ '/^.*(_log.csv)$/{ print $3;}'`
				if [ -z $is_log_file ]
				then
					continue
				else
					log_file=`echo $iterator_file | gawk -F/ '/^.*(_log)(.csv)$/{print $0;}'`
				fi
			done
			
			#获取app对应的package名
			process_name=$(get_process_name $not_log_file)
			#将app包名中的点号替换为\.
			process_name_escape_dot=`echo $process_name | sed -e 's/\./\\\./g'`
			process_fps=`calculate_fps $process_name_escape_dot $log_file`
			calculate_items $not_log_file $process_name $process_fps
		fi
	done
fi

